package com.bitsfromspace.ubertorrent;

import com.bitsfromspace.ubertorrent.bencoding.BEncoder;
import com.bitsfromspace.ubertorrent.bencoding.BValue;
import com.bitsfromspace.ubertorrent.utils.Sha1HashUtils;

/**
 * A helper class for piece hash.
 * @author: chris.oudeman
 * @since: Jan 5, 2011
 */
public class Sha1Hash {
	private final byte[] hash;

	protected Sha1Hash(byte[] hash) {

		this.hash = hash;
	}
	
	/**
	 * Returns the 20 byte SHA1 hash for a piece.
	 */
	public byte[] getHash() {

		return hash;
	}

	/**
	 * Calculates the Sha1Hash for parameter <em> bytes</em>
	 * @return Calculated {@link Sha1Hash}
	 */
	public static Sha1Hash calculate(byte[] bytes) {

		return new Sha1Hash(Sha1HashUtils.calculateHash(bytes));
	}

	/**
	 * Calculates the {@link Sha1Hash} for parameter <em>bValue</em>
	 * @return Calculated {@link Sha1Hash}
	 */
	public static Sha1Hash calculate(BValue bValue) {

		return calculate(BEncoder.encode(bValue));
	}

}


